package com.primb.androidlibs.net.override;

import android.widget.Toast;

import com.primb.androidlibs.net.exception.ApiException;
import com.primb.androidlibs.net.exception.ExceptionEngine;
import com.primb.androidlibs.utils.NetWorkUtils;
import com.primb.androidlibs.utils.ToastUtils;
import com.primb.androidlibs.utils.Utils;

import rx.Subscriber;


/**
 * Created by Chen on 2017/6/19.
 * 功能描述：
 */

public abstract class BaseSubscriber<T> extends Subscriber<T> {

    public BaseSubscriber() {
    }

    @Override
    public void onStart() {
        super.onStart();
        if (!NetWorkUtils.isNetworkConnected()) {
            ToastUtils.showShort("没有网络连接，请检查后重试");
            onCompleted();
            return;
        }
    }

    @Override
    public void onError(Throwable e) {
        ApiException apiException = ExceptionEngine.handleException(e);
        onError(apiException);
    }

    public void onError(ApiException e) {
        Toast.makeText(Utils.getApp(), e.getMessage(), Toast.LENGTH_SHORT).show();
    }
}
